package com.example.dirviesystem.Admin.controller;

import com.example.dirviesystem.Admin.pojo.StudentMessageData;
import com.example.dirviesystem.config.R;
import com.example.dirviesystem.entity.*;
import com.example.dirviesystem.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

@RestController
public class StudentAdminController {

    @Autowired
    private StudentInfoRe studentInfoRe;
    @Autowired
    private ApplicationFormRe applicationFormRe;
    @Autowired
    private StudentMessageRep studentMessageRep;
    @Autowired
    private CoachInfoRe coachInfoRe;
    @Autowired
    private DrivingProgressRepository drivingProgressRepository;


    @GetMapping("/getApplicationForms")
    public List<ApplicationForm> getApplicationForms(){
        boolean approved=false;
        return applicationFormRe.findByApproved(approved);
    }

    @PostMapping("/createStudentMessage")
    public R createCoachQualificationMessage(@RequestBody StudentMessageData studentMessageData){
        System.out.println(studentMessageData);

        ApplicationForm applicationForm = applicationFormRe.findById(studentMessageData.getApplicationId())
                .orElseThrow(() -> new RuntimeException("申请表不存在"));
        StudentInfo student = studentInfoRe.findById(studentMessageData.getStudentId())
                .orElseThrow(() -> new RuntimeException("学生不存在"));
        StudentMessage message = new StudentMessage();
        message.setStudent(student);
        message.setLook(false);
        message.setContent(studentMessageData.getMessageContent());
        message.setCreateTime(LocalDateTime.now());
        System.out.println(message);
        studentMessageRep.save(message);
        return R.success("消息发送成功");

    }
    @PostMapping("/approveApplication/{applicationId}")
    public R approveCoachQualification(@PathVariable Long applicationId){
        ApplicationForm applicationForm = applicationFormRe.findById(applicationId)
                .orElseThrow(() -> new RuntimeException("申请表不存在"));
        // 标记审核通过
        applicationForm.setApproved(true);
        applicationFormRe.save(applicationForm);
        // 同时更新学生信息中的资格状态
        StudentInfo student = applicationForm.getStudent();
        student.setApproved(true);
        studentInfoRe.save(student);
        return R.success("报名审核通过");

    }
    @PostMapping("/rejectApplication/{applicationId}")
    public R rejectCoachQualification(@PathVariable Long applicationId){
        ApplicationForm applicationForm = applicationFormRe.findById(applicationId)
                .orElseThrow(() -> new RuntimeException("申请表不存在"));
        // 标记审核不通过
        applicationForm.setApproved(false);
        applicationFormRe.save(applicationForm);
        // 同时更新学生信息中的资格状态
        StudentInfo student = applicationForm.getStudent();
        student.setApproved(false);
        studentInfoRe.save(student);

        return R.success("报名审核不通过");
    }
    @PostMapping("/assignCoachToStudent/{studentId}")
    public R assignCoachToStudent(@PathVariable Long studentId){
        StudentInfo student = studentInfoRe.findById(studentId)
                .orElseThrow(() -> new RuntimeException("学生不存在"));
        ApplicationForm applicationForm = applicationFormRe.findByStudentId(studentId);
        String drivingType = applicationForm.getDrivingType().getCode();
       List<CoachInfo> coachInfos = coachInfoRe.findAllByDrivingTypeCode(drivingType);
        List<CoachInfo> coachInfos1 =coachInfos.stream()
                .filter(coachInfo -> {
                    return coachInfo.getIsQualified();
                })
                .toList();
        // 随机选择一个教练
        Random random = new Random();
        int randomIndex = random.nextInt(coachInfos1.size());
        CoachInfo assignedCoach = coachInfos1.get(randomIndex);

        // 分配教练给学生
        student.setCoach(assignedCoach);

        // 保存学生信息
        studentInfoRe.save(student);
        applicationForm.setPreferredCoach(assignedCoach);

        applicationFormRe.save(applicationForm);
        DrivingProgress drivingProgress=new DrivingProgress();
        drivingProgress.setStudent(student);
        drivingProgressRepository.save(drivingProgress);

        return R.success("教练分配成功");
    }

    @PostMapping("/assignPreferCoachToStudent/{studentId}")
    public R assignPreferCoachToStudent(@PathVariable Long studentId,@RequestBody CoachInfo preferredCoach){

        System.out.println(preferredCoach);
        StudentInfo student = studentInfoRe.findById(studentId)
                .orElseThrow(() -> new RuntimeException("学生不存在"));
        CoachInfo assignedCoach = coachInfoRe.findById(preferredCoach.getId())
                .orElseThrow(() -> new RuntimeException("教练不存在"));
        // 分配教练给学生
        student.setCoach(assignedCoach);
        // 保存学生信息
        studentInfoRe.save(student);
        DrivingProgress drivingProgress=new DrivingProgress();
        drivingProgress.setStudent(student);

        drivingProgressRepository.save(drivingProgress);
        return R.success("教练分配成功");
    }

}
